package com.zyd.blog.personal.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zyd.blog.homeart.util.ResponseObject;
import com.zyd.blog.persistence.beans.TbUserNo;
import com.zyd.blog.personal.service.LoginService;
import com.zyd.blog.personal.service.TbUserNoService;
import com.zyd.blog.personal.service.UserInfoService;
import com.zyd.blog.personal.service.UserNoService;
import com.zyd.blog.util.RestClientUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserNoService userNoService;

    @Resource
    private TbUserNoService tbUserNoService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseObject login(String username, String password) {
        //查询本地，如果本地用户存在,并且被禁用,那就直接
        String   json= RestClientUtil.get("http://uct.wljiam.com/login?username="+username+"&password="+password);
        JSONObject jsonObject=JSONObject.parseObject(json);
        System.out.println(jsonObject);
        if(jsonObject.get("code").equals(0)){//登陆成功，检测用户是否已经存在，不存插入用户
            String data=  jsonObject.get("data").toString();
            JSONObject dataJson=JSONObject.parseObject(data);
            String userId=dataJson.getString("userId");
            TbUserNo tun=tbUserNoService.getByPrimaryKey(Long.parseLong(userId));
            if(null!=tun&&tun.getStatus().equals("0")){
                ResponseObject ren=new ResponseObject();
                ren.setCode(1);
                ren.setMsg("用户已禁用");
                return ren;
            }
            List<Map<String, Object>> re = userInfoService.getUserInfo(Long.parseLong(userId));
            if(re.size()<1){//第一次登陆查询用户中心数据
                String  userInfo= RestClientUtil.get("http://47.107.245.202:8281/getUserInfoById?userId="+userId);//获取用户中心用户数据
                JSONObject user=JSONObject.parseObject(userInfo).getJSONObject("user");
                String name=user.getString("userName");
                String headPic=user.getString("headPic");
                Integer sex=user.getInteger("sex");
                Map<String,Object> userMap=new HashMap<>();
                userMap.put("userId",Long.parseLong(userId));
                userMap.put("telephone",username);
                userMap.put("updateTime",new Date().getTime());
                userMap.put("headPicture",headPic);
                userMap.put("nickname",name);
                if(sex==null||sex==1){//判断前要加空值判断
                    userMap.put("sex","男");
                }else{
                    userMap.put("sex","女");
                }
                userInfoService.addUserInfo(userMap);
                Map<String,Object> userNoMap=new HashMap<>();
                userNoMap.put("userId",Long.parseLong(userId));
                userNoMap.put("username",username);
                userNoMap.put("userType",0);
                userNoMap.put("updateTime",new Date().getTime());
                userNoMap.put("addTime",new Date().getTime());
                userNoService.saveUserNo(userNoMap);
            }
        }
        return JSONObject.parseObject(json,ResponseObject.class);
    }
    @Override
    public ResponseObject checkToken(String token) {

        ResponseObject res=new ResponseObject();

        String req_url = "http://uct.wljiam.com/check?token=" + token;

        String ret = RestClientUtil.get(req_url);

        if(ret.contains("<html>")){
            res.setCode(1);
            res.setMsg("检测未通过");
            return res;
        }
        JSONObject object = JSON.parseObject(ret);
        if (object != null && object.get("gtoken") != null && object.get("gtoken").equals("ok")) {
            res.setCode(0);
            res.setMsg("检测通过");
        } else {
           res.setCode(1);
           res.setMsg("检测未通过");
        }

        return res;


    }
}